/**
 *公共JS
 */

var APPID = 'wxbdebf4bd5d1cf85b';
var BASE_URL = 'http://www.galaxy1234.com';
var REDIRECT_URI = window.location.pathname+window.location.search;
var HTML_SCOPE = 'snsapi_userinfo';

var winW = $(window).width();
var winH = $(window).height();
var user_id;
/**
 * 初始化全局页面
 */
function initGlobalPage() {
    //背景图

    var token = sessionStorage.access_token;    //获取存储token

    if (token == null || token == undefined) {    //判断是否需要存在token
        var code = getQueryString('code');  //获取code
        if (code == null) {
            getUserCode(1); //获取code
        } else {
            ajaxGetToken(code); //获取token
        }
    }
}

//点击关闭弹出层按钮
function onClosePopup(obj) {
    $(obj).parent().parent().hide();
    $('html,body').removeClass('ovfHiden'); //使网页可滚动
}

//点击关闭遮盖层，关闭弹出层
function onCloseClickPopup(obj) {
    $(obj).hide();
    $('html,body').removeClass('ovfHiden'); //使网页可滚动
}

//
$('.pop-up-box').click(function () {
    onCloseClickPopup($(this));
});

/**
 * 获取用户code
 * @param state
 */
function getUserCode(state) {
    var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + '&redirect_uri=' + BASE_URL + REDIRECT_URI + '&response_type=code&scope=' + HTML_SCOPE + '&state=' + state + '#wechat_redirect';
    window.location.href = url;
}

/**
 * 获取查询字符串
 * @param name
 * @returns {null}
 */
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

/**
 * 返回/刷新 页面
 * @param num
 */
function backPage(num) {
    history.go(num);
}

/**
 * 获取token
 * @param code
 */
function ajaxGetToken(code) {
    $.ajax({
        type: "get"
        , url: '/index/user/ajax_get_user_info'
        , data: {
            code: code
        }, async: false
        , success: function (req) {
            sessionStorage.setItem('access_token', req.data.token);
            //console.info(req);
        }
        , error: function (resq) {
            console.info(resq);
            layer.msg("服务器异常");
        }

    });
}

/**
 * 重新获取Token
 */
function againToken() {
    // 清除所有键值
    sessionStorage.clear();
    initGlobalPage();
}

/**
 * 获取系统配置信息
 * @param vue
 * @param token
 */
function initConfigInfo(vue, token) {
    var data = {
        token: token,
    };
    $.ajax({
        type: "get"
        , url: '/index/config/ajax_get_system_config'
        , data: data
        , async: false
        , success: function (result) {
            console.info(result);
            if (result.code == 1) {
                vue.config_info = result.data;
            }
        }
        , error: function (resq) {
            console.info(resq);
            layer.msg("服务器异常");
        }

    });
}

/**
 * 初始化用户信息
 */
function initUserInfo(vue, token) {
    var data = {
        token: token,
    };
    $.get("/index/user/ajax_get_user_info_token", data, function (result) {
        console.info(result);
        if (result.code == 0) {
            againToken();
        }
        vue.user_info = result.data;
        user_id = result.data.id;
    });
}

/**
 * 判断正整数
 * @param num
 * @returns {boolean}
 */
function isPositiveInteger(num) {
    if ((/^(\+|-)?\d+$/.test(num)) && num > 0) {
        return true;
    } else {
        return false;
    }
}

/**
 * 手动关闭弹出层
 * @param str
 */
function onSelfClosePopup(str) {
    $('#' + str).hide();
    $('html,body').removeClass('ovfHiden'); //使网页可滚动
}

function add0(m) {
    return m < 10 ? '0' + m : m
}
/**
 * 时间戳转换日期格式
 * @param val
 * @returns {string}
 */
function formatDate(val) {
    var time = new Date(val * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    //return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm);
}